using System;
using System.Net;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using Google.GData.YouTube;

namespace ScottLane.VideoInbox
{
    public class YouTubeVideo : DownloadableVideo
    {
        /// <summary>
        /// 
        /// </summary>
        private YouTubeVideo()
        {
        }

        #region Properties

        private string _author;

        /// <summary>
        /// Gets the video author.
        /// </summary>
        public string Author
        {
            get { return _author; }
        }

        /// <summary>
        /// Gets a value indicating whether the video has a high quality version available.
        /// </summary>
        public bool HighQualityAvailable
        {
            get { return Regex.Match(WebPageHtml, "watch in high quality", RegexOptions.Compiled).Success; }
        }

        private Uri _sourceLocationLowQuality;

        /// <summary>
        /// Gets the low quality video download uri.
        /// </summary>
        public Uri SourceLocationLowQuality
        {
            get
            {
                if (_sourceLocationLowQuality == null)
                {
                    Match match = Regex.Match(WebPageHtml, "\"video_id\": \"(?<video_id>.*?)\".*\"t\": \"(?<t>.*?)\"", RegexOptions.Compiled);
                    string url = "http://www.youtube.com/get_video?video_id=" + match.Groups["video_id"].Value + "&t=" + match.Groups["t"].Value;
                    _sourceLocationLowQuality = new Uri(url);
                }

                return _sourceLocationLowQuality;
            }
        }

        /// <summary>
        /// Gets the high quality video download uri.
        /// </summary>
        public Uri SourceLocationHighQuality
        {
            get { return new Uri(SourceLocationLowQuality, "&fmt=18"); }
        }

        private string _webPageUrl;

        /// <summary>
        /// Gets the video web page url.
        /// </summary>
        public string WebPageUrl
        {
            get { return _webPageUrl; }
        }

        private string _webPageHtml;

        /// <summary>
        /// Gets the html of the video web page.
        /// </summary>
        public string WebPageHtml
        {
            get
            {
                if (string.IsNullOrEmpty(_webPageHtml))
                {
                    _webPageHtml = new WebClient().DownloadString(_webPageUrl);
                }

                return _webPageHtml;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Creates a YouTubeVideo instance from a YouTubeEntry.
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public static YouTubeVideo Create(YouTubeEntry entry)
        {
            YouTubeVideo video = new YouTubeVideo();

            video._author = entry.Authors[0].Name;
            video.Title = entry.Title.Text;
            video.Created = entry.Published;
            video._webPageUrl = entry.AlternateUri.ToString();
            video.ThumbnailUrl = entry.Media.Thumbnails[0].Url;

            return video;
        }

        #endregion

        //private string _shortcutPath;

        ///// <summary>
        ///// Gets or sets the path of an additional shortcut that will point at the video. 
        ///// </summary>
        //public string ShortcutPath
        //{
        //    get { return _shortcutPath; }
        //    set { _shortcutPath = value; }
        //}
    }
}
